package com.cy.detectsys.util;

import cn.hutool.core.util.StrUtil;
import com.cy.detectsys.config.CustomException;
import lombok.extern.slf4j.Slf4j;
import net.lingala.zip4j.ZipFile;
import net.lingala.zip4j.exception.ZipException;
import net.lingala.zip4j.model.ZipParameters;
import net.lingala.zip4j.model.enums.CompressionMethod;
import net.lingala.zip4j.model.enums.EncryptionMethod;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.Deflater;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import static com.cy.detectsys.util.FileOperateUtil.deleteFolder;

@Slf4j
public class ZipUtil {

    /**
     * 遍历压缩文件夹
     *
     * @param folderPath
     * @param password
     * @throws IOException
     * @throws ZipException
     */
    public static void zipFolder(String folderPath, String password) throws IOException, ZipException {


        File pathFile = new File(folderPath);
        if (!pathFile.exists()) {
            throw new CustomException("压缩路径不存在");
        }
        if (pathFile.isDirectory()) {
            try {
                recursiveDeflateZip(pathFile, password);
            } catch (ZipException e) {
                throw new CustomException("文件夹压缩失败");
            }
        } else {
            try {
                singleDeflateZip(pathFile, password);

            } catch (ZipException e) {
                throw new CustomException("文件压缩失败");
            }
        }
    }


    private static void zipFolder(String folderPath) throws ZipException, IOException {
        zipFolder(folderPath, ""); // 不设置密码的压缩文件夹
    }


    /**
     * 遍历压缩文件夹  并删除文件夹
     *
     * @param folderPath
     * @param deleteOriginFolder
     */
    public static void zipFolder(String folderPath, Boolean deleteOriginFolder) throws IOException, ZipException {
        zipFolder(folderPath);  //压缩
        if (deleteOriginFolder) {
            deleteFolder(folderPath);  //删除文件夹
        }
    }

    /**
     * 遍历压缩文件夹  并删除文件夹
     *
     * @param folderPath
     * @param deleteOriginFolder
     */
    public static void zipFolder(String folderPath, Boolean deleteOriginFolder, String password) throws IOException, ZipException {
        zipFolder(folderPath, password);  //压缩
        if (deleteOriginFolder) {
            deleteFolder(folderPath);  //删除文件夹
        }
    }


    public static R doDeflateZip(String path, String password) {
        File pathFile = new File(path);
        if (!pathFile.exists()) {
            throw new CustomException("压缩路径不存在");
        }
        if (pathFile.isDirectory()) {
            try {
                recursiveDeflateZip(pathFile, password);
            } catch (ZipException e) {
                throw new CustomException("文件夹压缩失败");
            }
        } else {
            try {
                singleDeflateZip(pathFile, password);

            } catch (ZipException e) {
                throw new CustomException("文件压缩失败");
            }
        }
        return R.ok("文件压缩成功");
    }


    public static String recursiveDeflateZip(File pathFile, String password) throws ZipException {
        ZipParameters zipParameters = new ZipParameters();
        zipParameters.setEncryptFiles(true);
        zipParameters.setCompressionMethod(CompressionMethod.DEFLATE);//设置压缩方法是deflate
        zipParameters.setEncryptionMethod(EncryptionMethod.ZIP_STANDARD);
        new ZipFile(pathFile + ".zip", password.toCharArray()).addFolder(pathFile, zipParameters);
        return pathFile + ".zip";
    }

    public static void singleDeflateZip(File pathFile, String password) throws ZipException {
        ZipParameters zipParameters = new ZipParameters();
        zipParameters.setCompressionMethod(CompressionMethod.DEFLATE);//设置压缩方法是deflate
        zipParameters.setEncryptFiles(true);
        zipParameters.setEncryptionMethod(EncryptionMethod.ZIP_STANDARD);
        new ZipFile(pathFile + ".zip", password.toCharArray()).addFile(pathFile, zipParameters);
    }

}
